Exercises
Product Details page
Let's suppose we're building an e-commerce site. We received a mockup, and we implemented it as one big component.
In terms of our spectrum of abstraction, we've created a very high-level component, but there are two lower-level components hiding within, begging to be extracted into their own components.
Your mission is to identify a spectrum-related problem, and to extract at least 1 lower-level component.
Acceptance Criteria:
- Extract at least 1 lower-level, potentially-reusable component
- The extracted component(s) can be defined inside
/ProductDetails.js
, below the main component. - If you're not sure where to start / which components should be extracted, I provide my suggestions below the sandbox:
Code Playground
- Warning: Each child in a list should have a unique "key" prop. Check the render method of `ProductDetails`. See https://reactjs.org/link/warning-keys for more information. at eval (https://sandpack-bundler.vercel.app/node_modules/react-feather/dist/icons/star.js:51:25) at ProductDetails (https://sandpack-bundler.vercel.app/ProductDetails.js:26:3) at App
Solution:
Extracting a Card component
In the sandbox below, we have two separate components that each implement a similar "card" design. Let's extract it into its own Card
component!
Acceptance Criteria:
- Two files,
Card.js
andCard.module.css
, have been created for you. Your mission is to populate them with the component + styles. - You should then use this component within
UserProfileCard
andProductInfoCard
. - Note that the two cards have slightly different styles: they have a different
box-shadow
value. Each card should be able to specify the elevation level for the shadow, to be applied dynamically insideCard
.
Code Playground
Solution:
In this video, I use the “property value shorthand” when applying the boxShadow
style. For more information, check out the “Property Value Shorthand” lesson 👀 in the JavaScript Primer.